from playwright.sync_api import Playwright, sync_playwright, expect
from bs4 import BeautifulSoup
import pandas as pd

def run(playwright: Playwright, place: str, time_str1: str, time_str2: str) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.wentian123.com/jiangxi/")
    with page.expect_popup() as page1_info:
        page.get_by_role("link", name=place).click()
    page1 = page1_info.value
    page1.get_by_placeholder("开始日期").fill(time_str1)
    page1.get_by_placeholder("结束日期").fill(time_str2)
    page1.locator("input[type=\"submit\"]").click()
    # playwright加载网址，点击页面跳转，并自动等待页面加载完毕，然后将当前页面保存为html
    # 读取html文件中的table内容
    page1.wait_for_timeout(2000)
    html = page1.content()
    soup = BeautifulSoup(html, 'html.parser')
    table = soup.find('table') # 找到table标签
    trs = table.find_all('tr') # 找到所有的tr标签，即表格中的每一行数据 
    data = [] # 存放表格中的数据 
    for tr in trs:  # 遍历每一行数据 
        tds = tr.find_all('td') # 找到表格中的所有td标签，即每一列数据 
        row = [td.text for td in tds] # 取出表格中的文本信息 
        data.append(row) # 将数据存入data中 

    # 利用pandas将list转化为DataFrame  
    df = pd.DataFrame(data, columns=["时间", "天气", "气温", "风"])
    df.to_excel(f"./data/04气象历史数据/{place}_{time_str1}_{time_str2}.xlsx")
    page1.close()
    page.close()

    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    places = ["上饶", "鄱阳", "余干"]
    time_str1 = "2023-01-01"
    time_str2 = "2023-01-31"
    for place in places:
        run(playwright, place, time_str1, time_str2)
